package com.ruoyi.coin.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.coin.mapper.CoinBaseSqlMapper;
import com.ruoyi.coin.service.ICoinBaseSqlService;
import com.ruoyi.common.utils.blade.DataTypeUtil;
import com.ruoyi.common.utils.blade.Func;
import com.ruoyi.common.utils.blade.tool.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class CoinBaseSqlServiceImpl implements ICoinBaseSqlService {


    @Autowired
    private CoinBaseSqlMapper coinBaseSqlMapper;


    private String getSelectField() {
        return "*";
    }


    //根据id获取某一个表的一条数据 进行过滤
    @Override
    public List<Map<String, Object>> getDataListByField(String tableName, String whereFieldName, Object whereFieldValue) {
        return coinBaseSqlMapper.getDataListByField(this.getSelectField(), tableName, whereFieldName, whereFieldValue);
    }

    //获取一条数据
    @Override
    public Map<String, Object> getDataOneByField(String tableName, String whereFieldName, Object whereFieldValue) {
        List<Map<String, Object>> dataList = this.getDataListByField(tableName, whereFieldName, whereFieldValue);
        if (Func.isEmpty(dataList)) {
            return null;
        }
        return dataList.get(0);
    }

    //单个新增
    @Override
    public Long baseInsertData(String tableName, Map<String, Object> map) {
        map.put("create_time", DateUtil.now());

        String feildSql = "";
        String dataSql = "";
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String field = entry.getKey();
            Object value = entry.getValue();
            feildSql += field + ",";
            dataSql += DataTypeUtil.getSql(field, value) + ",";
        }
        if (feildSql.endsWith(",")) {
            feildSql = feildSql.substring(0, feildSql.length() - 1);
            dataSql = dataSql.substring(0, dataSql.length() - 1);
        }
        String sql = "insert into " + tableName + "(" + feildSql + ") values (" + dataSql + ")";

        map.put("select_sql", sql);
        Long result = coinBaseSqlMapper.baseInsertSql(map);
        return result;
    }

    //单个修改
    @Override
    public void baseUpdateDataLong(String tableName, Map<String, Object> map, Long id) {
        map.put("update_time", DateUtil.now());
        String updateSql = "update " + tableName + " set ";
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String field = entry.getKey();
            Object value = entry.getValue();
            if (Func.equals("id", field)) {
                continue;
            }

            updateSql += field + " = " + DataTypeUtil.getSql(field, value) + ",";
        }
        if (updateSql.endsWith(",")) {
            updateSql = updateSql.substring(0, updateSql.length() - 1);
        }
        String sql = updateSql + " where id =" + id;
        map.put("select_sql", sql);
        coinBaseSqlMapper.baseUpdateSql(map);
    }

    // 根据表属性获取所有数据 多条件
    @Override
    public List<Map<String, Object>> getDataListByWrapper(String tableName, Wrapper wrapper) {
        if (Func.isEmpty(tableName)) {
            return null;
        }
        return coinBaseSqlMapper.getDataListByWrapper(tableName, wrapper);
    }

    // 根据表属性获取所有数据 多条件 分页
    @Override
    public IPage<Map<String, Object>> getDataIPageWrapper(String tableName, IPage page, Wrapper wrapper) {
        if (Func.isEmpty(tableName)) {
            return null;
        }

        IPage<Map<String, Object>> pages = coinBaseSqlMapper.getDataListByWrapper(tableName, page, wrapper);
        return pages;
    }



}
